<template>
	<view>
		<view class="head-top">
			<view style="height: 4vh">
			</view>
			<view class="head-top-row">
				<view class="head-top-row-right" @click="Hui">
					<返回 </view>
						<view class="head-top-row-left">
							<view class="head-top-row-left-text">
								物料接收
							</view>
						</view>
				</view>
			</view>
			<view class="head-body">
				<uni-section title="工单信息" type="line" titleFontSize="20px">
					<uni-row class="demo-uni-row" :width="nvueWidth">
						<uni-col :span="4">
							<view class="gd-text">MO号:</view>
						</uni-col>
						<uni-col :span="13">
							<uni-easyinput trim="all" v-model="mo" placeholder="请输入内容" @input="input"></uni-easyinput>
						</uni-col>
						<uni-col :span="7">
							<yt-scanCode btnName="扫MO/SN" style="margin-left: 4px;margin-top: 3px;"
								@getScanCode="OnScanningCode"></yt-scanCode>
						</uni-col>
					</uni-row>
					<uni-row class="demo-uni-row" :width="nvueWidth">
						<uni-col :span="4">
							<view class="gd-text">工序:</view>
						</uni-col>
						<uni-col :span="20">
							<uni-data-select style="width: 98%;" v-model="production" :localdata="range"
								@change="change"></uni-data-select>
						</uni-col>
					</uni-row>
				</uni-section>
				<view style="border-top: 1px solid #cecece; margin-top: 30px;">
				</view>
				<uni-section title="物料信息" type="line" titleFontSize="20px">
					<template v-slot:right>
						<uni-row>
							<uni-col :span="4.8">
								<button type="primary" style="margin-right: 4px; margin-top: 10px;" size="mini"
									@click="HandMovement">手动</button>
							</uni-col>
							<uni-col :span="5.7">
								<button class="getScanCode" type="primary" size="mini"
									@click="ScanMaterials">扫物料</button>
								<!-- <yt-scanCode btnName="扫物料" style="margin-right: 4px;margin-top: 10px;"
									@getScanCode="ScanMaterials"></yt-scanCode> -->
							</uni-col>
							<uni-col :span="4.5">
								<button type="primary" style="margin-top: 10px;" size="mini" @click="save">保存</button>
							</uni-col>
						</uni-row>
					</template>
					<uni-card>
						<view style="height:50vh">
							<zb-table :columns="column" :stripe="true" :border="true" :data="data"></zb-table>
						</view>
					</uni-card>
				</uni-section>
			</view>
			<uni-popup ref="inputDialog" type="dialog">
				<uni-popup-dialog ref="inputClose" mode="input" title="物料信息" :before-close="true"
					@confirm="dialogInputConfirm" @close="dialogInputConfirmclose">
					<uni-forms ref="customForm" :rules="customRules" labelWidth="90px" :modelValue="customFormData">
						<uni-forms-item label="包装码：" name="PackagingCode">
							<uni-easyinput v-model="customFormData.PackagingCode" placeholder=""
								@input="PackagingCodeInput" />
						</uni-forms-item>
						<uni-forms-item label="料号：" name="PartNumber">
							<uni-easyinput v-model="customFormData.PartNumber" placeholder=""
								@input="PartNumberInput" />
						</uni-forms-item>
						<uni-forms-item label="需求数量：" name="DemandQuantity">
							<uni-easyinput v-model="customFormData.DemandQuantity" placeholder="" :disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="实收数量：" name="lastreciveNum">
							<uni-easyinput v-model="customFormData.lastreciveNum" placeholder="" :disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="核对数量：" name="reciveNum">
							<uni-easyinput v-model="customFormData.reciveNum" placeholder="" />
						</uni-forms-item>
					</uni-forms>
				</uni-popup-dialog>
			</uni-popup>
			<uni-popup ref="Dialog" type="dialog">
				<uni-popup-dialog ref="inputClose" mode="input" title="物料信息" :before-close="true"
					@confirm="dialogInputConfirm" @close="dialogInputConfirmclose">
					<uni-forms ref="customForm" :rules="customRules" labelWidth="90px" :modelValue="customFormData">
						<uni-forms-item label="包装码：" name="PackagingCode">
							<uni-easyinput v-model="customFormData.PackagingCode" placeholder="" :disabled="true"
								@input="PackagingCodeInput" />
						</uni-forms-item>
						<uni-forms-item label="料号：" name="PartNumber">
							<uni-easyinput v-model="customFormData.PartNumber" placeholder="" :disabled="true"
								@input="PartNumberInput" />
						</uni-forms-item>
						<uni-forms-item label="需求数量：" name="DemandQuantity">
							<uni-easyinput v-model="customFormData.DemandQuantity" placeholder="" :disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="实收数量：" name="lastreciveNum">
							<uni-easyinput v-model="customFormData.lastreciveNum" placeholder="" :disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="核对数量：" name="reciveNum">
							<uni-easyinput v-model="customFormData.reciveNum" placeholder="" />
						</uni-forms-item>
					</uni-forms>
				</uni-popup-dialog>
			</uni-popup>
			<sl-pda-employ :config="pdaConfig"></sl-pda-employ>
		</view>
</template>

<script>
	import {
		MaterialReceivingQuery,
		MaterialInformationQuery,
		MaterialInformationSave
	} from '@/api/system/MaterialReceiving'
	import slPdaEmploy, {
		initPdaScan,
		manualTrigScan,
		onUnloadPdaScan,
		BroadcastConfig
	} from '../../uni_modules/sl-pdaEmploy'
	export default {
		data() {
			return {
				pdaConfig: { // 可以抽离出去管理这个配置
					BroadcastAction: 'nantong',
					BroadcastLabelKey: 'data',
					//BroadcastTriglKey: 'com.scan.onStartScan',
				},
				nvueWidth: uni.getWindowInfo().screenWidth,
				mo: "",
				production: "",
				range: [],
				data: [],
				ScanMaterialsText: "",
				customRules: {
					PackagingCode: {
						rules: [{
							required: true,
							errorMessage: '包装码不能为空'
						}]
					},
					PartNumber: {
						rules: [{
							required: true,
							errorMessage: '料号不能为空'
						}]
					},
					reciveNum: {
						rules: [{
							required: true,
							errorMessage: '核对数量不能为空'
						}, {
							format: 'number',
							errorMessage: '核对数量只能输入数字'
						}]
					}
				},
				customFormData: {
					PackagingCode: "",
					PartNumber: "",
					DemandQuantity: "",
					reciveNum: "",
					lastreciveNum: "",
					jobId: "",
					mateId: "",
					mateCode: "",
					mateName: "",
					mateDesignNumber: "",
					mateType: "",
					reciveNum: "",
					reciveId: "",
					reviceCode: "",
					reciveName: "",
					packagingCode: "",
					reqId: "",
				},
				column: [{
						label: '序号',
						type: 'index',
						width: uni.getWindowInfo().screenWidth / 10 * 1
					},
					{
						name: 'PartNumber',
						label: '料号',
						width: uni.getWindowInfo().screenWidth / 10 * 2.5
					},
					{
						name: 'DemandQuantity',
						label: '需求数量',
						width: uni.getWindowInfo().screenWidth / 10 * 2.14
					},
					{
						name: 'reciveNum',
						label: '核对数量',
						width: uni.getWindowInfo().screenWidth / 10 * 2.14
					},
					{
						name: 'PackagingCode',
						label: '包装码',
						width: uni.getWindowInfo().screenWidth / 10 * 2
					}
				]
			}
		},
		components: {
			slPdaEmploy
		},
		onLoad() {
			initPdaScan(code => {
				//console.log(code)
				if (this.production !== "") {
					this.customFormData.PackagingCode = code;
					this.customFormData.packagingCode = code;
					this.onScanMaterialsDialog(code);
				} else {
					this.mo = code;
					this.OnScanningCodeM0();
				}
			})
		},
		onUnload() {
			onUnloadPdaScan()
		},
		methods: {
			Hui() {
				this.$tab.reLaunch('/pages/home')
			},
			OnScanningCodeM0() {
				this.range = [];
				MaterialReceivingQuery(this.mo).then(res => {
					for (var i = 0; i < res.data.length; i++) {
						let a = {
							value: res.data[i].jobId,
							text: res.data[i].jobCode + "_" + res.data[i].jobName
						};
						this.range.push(a);
					}
					console.log("MO/SN查询", res.data)
				}).catch(error => {
					reject(error)
				})
			},
			OnScanningCode(res) {
				this.range = [];
				if (res === "") {

				} else {
					this.mo = res;
					this.production = "";
					MaterialReceivingQuery(res).then(res => {
						for (var i = 0; i < res.data.length; i++) {
							let a = {
								value: res.data[i].jobId,
								text: res.data[i].jobCode + "_" + res.data[i].jobName
							};
							this.range.push(a);
						}
						console.log("MO/SN查询", res.data)
					}).catch(error => {
						reject(error)
					})
				}
			},
			input(e) {
				this.production = "";
				this.range = [];
				if (e === '') {
					this.range = [];
				} else {
					MaterialReceivingQuery(e).then(res => {
						console.log("MO/SN查询", res)
						for (var i = 0; i < res.data.length; i++) {
							let a = {
								value: res.data[i].jobId,
								text: res.data[i].jobCode + "_" + res.data[i].jobName
							};
							this.range.push(a);
						}
					}).catch(error => {
						reject(error)
					})
				}
			},
			PackagingCodeInput(e) {
				if (e === "") {
					//this.clear();
				} else {
					MaterialInformationQuery(e, this.production).then(res => {
						console.log("包装码查询", res.data)
						if (res.data.length > 0) {
							//this.customFormData.PackagingCode = res.data[0].packagingCode;
							this.customFormData.PartNumber = res.data[0].mateCode;
							this.customFormData.DemandQuantity = res.data[0].planNum;
							this.customFormData.reciveNum = res.data[0].planNum * 1 - res.data[0].reciveNum * 1;
							this.customFormData.lastreciveNum = res.data[0].reciveNum;
							this.customFormData.jobId = res.data[0].jobId;
							this.customFormData.mateId = res.data[0].mateId;
							this.customFormData.mateCode = res.data[0].mateCode;
							this.customFormData.mateName = res.data[0].mateName;
							this.customFormData.mateDesignNumber = res.data[0].mateDesignNumber;
							this.customFormData.mateType = res.data[0].matetType;
							this.customFormData.reciveId = this.$store.state.user.userAll.userId,
								this.customFormData.reviceCode = this.$store.state.user.userAll.userCode,
								this.customFormData.reciveName = this.$store.state.user.userAll.userName,
								//this.customFormData.packagingCode = res.data[0].packagingCode,
								this.customFormData.reqId = res.data[0].id;
						} else {
							// //this.$modal.msgError("未查询到物料")
							// this.customFormData = {
							// 	PartNumber: "",
							// 	DemandQuantity: "",
							// 	reciveNum: "",
							// 	lastreciveNum: "",
							// 	jobId: "",
							// 	mateId: "",
							// 	mateCode: "",
							// 	mateName: "",
							// 	mateDesignNumber: "",
							// 	mateType: "",
							// 	reciveNum: "",
							// 	reciveId: "",
							// 	reviceCode: "",
							// 	reciveName: "",
							// 	reciveDate: "",
							// 	packagingCode: "",
							// 	reqId: "",
							// };
						}
					}).catch(error => {
						reject(error)
					})
				}

			},
			//获取年月日 时分秒
			// NyrTime() {
			// 	const date = new Date();
			// 	var year = date.getFullYear() // 年
			// 	var month = date.getMonth() + 1; // 月
			// 	var day = date.getDate(); // 日
			// 	var hours = date.getHours();
			// 	var minutes = date.getMinutes();
			// 	var seconds = date.getSeconds();
			// 	month = month < 10 ? '0' + month : month;
			// 	day = day < 10 ? '0' + day : day;
			// 	hours = hours < 10 ? '0' + hours : hours;
			// 	minutes = minutes < 10 ? '0' + minutes : minutes;
			// 	seconds = seconds < 10 ? '0' + seconds : seconds;
			// 	const time = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
			// 	return time;
			// },
			PartNumberInput(e) {
				if (e === "") {
					this.clear();
				} else {
					MaterialInformationQuery(e, this.production).then(res => {
						console.log("物料查询", res.data)
						if (res.data.length > 0) {
							this.customFormData.PackagingCode = this.customFormData.PartNumber;
							this.customFormData.PartNumber = res.data[0].mateCode;
							this.customFormData.DemandQuantity = res.data[0].planNum;
							this.customFormData.reciveNum = res.data[0].planNum * 1 - res.data[0].reciveNum * 1;
							this.customFormData.lastreciveNum = res.data[0].reciveNum;
							this.customFormData.jobId = res.data[0].jobId;
							this.customFormData.mateId = res.data[0].mateId;
							this.customFormData.mateCode = res.data[0].mateCode;
							this.customFormData.mateName = res.data[0].mateName;
							this.customFormData.mateDesignNumber = res.data[0].mateDesignNumber;
							this.customFormData.mateType = res.data[0].matetType;
							this.customFormData.reciveId = this.$store.state.user.userAll.userId,
								this.customFormData.reviceCode = this.$store.state.user.userAll.userCode,
								this.customFormData.reciveName = this.$store.state.user.userAll.userName,
								this.customFormData.packagingCode = this.customFormData.PartNumber,
								this.customFormData.reqId = res.data[0].id;
						} else {
							this.customFormData = {
								PackagingCode: "",
								DemandQuantity: "",
								reciveNum: "",
								lastreciveNum: "",
								jobId: "",
								mateId: "",
								mateCode: "",
								mateName: "",
								mateDesignNumber: "",
								mateType: "",
								reciveNum: "",
								reciveId: "",
								reviceCode: "",
								reciveName: "",
								reciveDate: "",
								packagingCode: "",
								reqId: "",
							};
						}
					}).catch(error => {
						reject(error)
					})
				}
			},
			change(e) {
				this.clear();
			},
			HandMovement() {
				if (this.production === "") {
					this.$modal.msgError("请选择工序")
				} else {
					this.$refs.inputDialog.open()
				}
			},
			ScanMaterials(res) {
				if (this.production === "") {
					this.$modal.msgError("请选择工序")
				} else {
					let that = this;
					uni.scanCode({
						onlyFromCamera: true,
						scanType: ['qrCode', 'barCode', 'dataMatrix'],
						success: res => {
							if (res.errMsg == 'scanCode:ok') {
								console.log(res.result)
								that.ScanMaterialsText = res.result;
								this.customFormData.PackagingCode = res.result;
								this.customFormData.packagingCode = res.result,
									that.ScanMaterialsDialog();
							} else {
								uni.showToast({
									title: '扫码失败',
									icon: "none",
									mask: true,
								})
							}
						},
						fail: err => {
							console.log(JSON.stringify(err))
						}
					})
				}
			},
			onScanMaterialsDialog(ScanMaterials) {
				console.log(ScanMaterials, this.production)
				MaterialInformationQuery(ScanMaterials, this.production).then(res => {
					console.log("物料查询", res.data)
					if (res.data.length > 0) {
						console.log("物料table", this.data)
						let sum = 0;
						for (var i = 0; i < this.data.length; i++) {
							if (this.data[i].reqId === res.data[0].id) {
								sum = sum + this.data[i].reciveNum * 1;
							}
						}
						//this.customFormData.PackagingCode = res.data[0].packagingCode;
						this.customFormData.PartNumber = res.data[0].mateCode;
						this.customFormData.DemandQuantity = res.data[0].planNum;
						this.customFormData.reciveNum = res.data[0].planNum * 1 - res.data[0].reciveNum * 1 - sum *
							1;
						this.customFormData.lastreciveNum = res.data[0].reciveNum * 1 + sum * 1;
						this.customFormData.jobId = res.data[0].jobId;
						this.customFormData.mateId = res.data[0].mateId;
						this.customFormData.mateCode = res.data[0].mateCode;
						this.customFormData.mateName = res.data[0].mateName;
						this.customFormData.mateDesignNumber = res.data[0].mateDesignNumber;
						this.customFormData.mateType = res.data[0].matetType;
						this.customFormData.reciveId = this.$store.state.user.userAll.userId,
							this.customFormData.reviceCode = this.$store.state.user.userAll.userCode,
							this.customFormData.reciveName = this.$store.state.user.userAll.userName,
							//this.customFormData.packagingCode = res.data[0].packagingCode,
							this.customFormData.reqId = res.data[0].id;
						this.$refs.Dialog.open();
					} else {
						this.$modal.msgError("未查询到物料")
					}
				}).catch(error => {
					reject(error)
				})
			},
			ScanMaterialsDialog() {
				MaterialInformationQuery(this.ScanMaterialsText, this.production).then(res => {
					console.log("物料查询", res.data)

					if (res.data.length > 0) {
						console.log("物料table", this.data)
						let sum = 0;
						for (var i = 0; i < this.data.length; i++) {
							if (this.data[i].reqId === res.data[0].id) {
								sum = sum + this.data[i].reciveNum * 1;
							}
						}
						//this.customFormData.PackagingCode = res.data[0].packagingCode;
						this.customFormData.PartNumber = res.data[0].mateCode;
						this.customFormData.DemandQuantity = res.data[0].planNum;
						this.customFormData.reciveNum = res.data[0].planNum * 1 - res.data[0].reciveNum * 1 - sum *
							1;
						this.customFormData.lastreciveNum = res.data[0].reciveNum * 1 + sum * 1;
						this.customFormData.jobId = res.data[0].jobId;
						this.customFormData.mateId = res.data[0].mateId;
						this.customFormData.mateCode = res.data[0].mateCode;
						this.customFormData.mateName = res.data[0].mateName;
						this.customFormData.mateDesignNumber = res.data[0].mateDesignNumber;
						this.customFormData.mateType = res.data[0].matetType;
						this.customFormData.reciveId = this.$store.state.user.userAll.userId,
							this.customFormData.reviceCode = this.$store.state.user.userAll.userCode,
							this.customFormData.reciveName = this.$store.state.user.userAll.userName,
							//this.customFormData.packagingCode = res.data[0].packagingCode,
							this.customFormData.reqId = res.data[0].id;
						this.$refs.Dialog.open();
					} else {
						this.$modal.msgError("未查询到物料")
					}
				}).catch(error => {
					reject(error)
				})
			},
			save() {
				if (this.data.length > 0) {
					MaterialInformationSave(this.data).then(res => {
						console.log("物料保存", res)
						if (res.data === '成功') {
							this.$modal.msgSuccess("保存成功")
						} else {
							uni.showToast({
								title: res.data,
								icon: "none",
								mask: true,
							})
						}
						this.data = [];
					}).catch(error => {
						this.data = [];
						reject(error)
					})
				} else {
					this.$modal.msgError("请添加物料")
				}

			},
			dialogInputConfirmclose() {
				this.$refs.inputDialog.close();
				this.$refs.Dialog.close();
				this.clear();
			},
			dialogInputConfirm() {
				this.$refs["customForm"].validate().then(res => {
					// let sum = 0;
					// for (var i = 0; i < this.data.length; i++) {
					// 	if (this.data[i].reqId === this.customFormData.reqId) {
					// 		sum = sum + this.data[i].reciveNum * 1;
					// 	}
					// }
					//console.log(sum)
					if (this.customFormData.reciveNum * 1 + this.customFormData.lastreciveNum * 1 >
						this.customFormData.DemandQuantity * 1) {
						this.$modal.msgError(this.customFormData.reciveNum * 1 + this.customFormData
							.lastreciveNum * 1 + "大于需求数量")
					} else {
						this.data.push(this.customFormData);
						this.clear();
						this.$refs.inputDialog.close();
						this.$refs.Dialog.close();
					}
				}).catch(err => {
					console.log('err', err);
				})

			},
			clear() {
				this.customFormData = {
					PackagingCode: "",
					PartNumber: "",
					DemandQuantity: "",
					reciveNum: "",
					lastreciveNum: "",
					jobId: "",
					mateId: "",
					mateCode: "",
					mateName: "",
					mateDesignNumber: "",
					mateType: "",
					reciveNum: "",
					reciveId: "",
					reviceCode: "",
					reciveName: "",
					reciveDate: "",
					packagingCode: "",
					reqId: "",
				};
			}
		}
	}
</script>

<style lang="scss">
	.head-top {
		height: 10vh;
	}

	.head-top-row {
		background-color: #007AFF;
		display: flex;
		height: 6vh;
	}

	.head-top-row-left {
		padding-top: 0.5vh;
		font-size: 19px;
		font-weight: 500;
		width: 86%;
	}

	.head-top-row-left-text {
		padding-left: 30%;
		color: #ffffff;
	}

	.head-top-row-right {
		color: #ffffff;
		margin-left: 2%;
		width: 14%;
		font-size: 17px;
		font-weight: 500;
		padding-top: 1vh;
	}

	.head-body {
		background-color: #ffffff;
		height: 90vh;
	}

	.demo-uni-row {
		padding-left: 10px;
		margin-top: 6px;
	}

	.gd-text {
		font-size: 17px;
		margin-top: 4px;
		float: right;
		margin-right: 6px;
	}

	::v-deep {
		.uni-card {
			margin: 1% !important;
			padding: 0 !important;

			.uni-card__content {
				padding: 0 !important;
			}
		}
	}

	.getScanCode {
		margin-right: 4px;
		margin-top: 10px;
	}

	::v-deep .is-disabled {
		color: black !important;
	}
</style>